import { useEffect, useState, useMemo } from 'react';
import { formatTime } from './index';

const useTimer = () => {
  const [sjc, setSjc] = useState<number>(0);

  useEffect(() => {
    setSjc(new Date().getTime());
    const dsq = setInterval(() => {
      setSjc(new Date().getTime());
    }, 1000);
    return () => {
      clearInterval(dsq);
    };
  }, []);

  const timer = useMemo(() => {
    return formatTime(sjc);
  }, [sjc]);

  return timer;
};

export default useTimer;
